package me.lzl.myblog.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.lzl.myblog.dao.ArticleDAO;
import me.lzl.myblog.domain.Article;
import me.lzl.myblog.domain.Page;

/**
 * 首页控制器
 * 
 * @author 梁子力
 *
 */
@WebServlet("/index.do")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.从SiteDAO获取站点信息

		ArticleDAO articleDAO = new ArticleDAO();
		try {

			int pageNo = 0;
			String pageNoStr = request.getParameter("page");
			if (pageNoStr != null && !"".equals(pageNoStr)) {
				pageNo = Integer.valueOf(pageNoStr);
			}

			Page<Article> page = articleDAO.findAll(pageNo);
			List<Article> articleBannerList = articleDAO.findFeaturedFirst(4);
			// 2.将站点信息保存到请求域，

			request.setAttribute("page", page);
			request.setAttribute("articleBannerList", articleBannerList);
			// 3.请求转发到index.jsp
			request.getRequestDispatcher("/front/index.jsp").forward(request, response);
		} catch (SQLException e) {

			e.printStackTrace();
		}

	}

}
